﻿Imports System.Data.SqlClient

Public Class frmTrialBalanceComparison

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim das As New DataSet
            das.Tables.Add("Transactions")
            das.Tables(0).Columns.Add("DestSource", GetType(String))
            das.Tables(0).Columns.Add("CustName", GetType(String))
            das.Tables(0).Columns.Add("TransType", GetType(String))
            das.Tables(0).Columns.Add("Writting", GetType(String))
            das.Tables(0).Columns.Add("Acc1", GetType(String))
            das.Tables(0).Columns.Add("Acc2", GetType(String))
            das.Tables(0).Columns.Add("TotalIn", GetType(Double))
            das.Tables(0).Columns.Add("TotalOut", GetType(Double))

            Dim cmd As New SqlCommand("Select Distinct Acc1,Acc2 From Transactions", cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                Dim row As String() = {Me.DTPPeriodFRm.Value.ToShortDateString, Me.DTPPeriodTo.Value.ToShortDateString, _
                                       Me.DTPPeriodFRm2.Value.ToShortDateString, Me.DTPPeriodTo2.Value.ToShortDateString, _
                                       Reader.Item(0).ToString, Reader.Item(1).ToString, _
                                       GetAccTransBalance(Reader.Item(0).ToString, Reader.Item(1).ToString, _
                                                          Me.DTPPeriodFRm.Value.ToShortDateString, Me.DTPPeriodTo.Value.ToShortDateString), _
                                       GetAccTransBalance(Reader.Item(0).ToString, Reader.Item(1).ToString, _
                                                          Me.DTPPeriodFRm2.Value.ToShortDateString, Me.DTPPeriodTo2.Value.ToShortDateString)}
                das.Tables(0).Rows.Add(row)
            End While
            cnn.Close()

            Dim rpt As New TrialBalanceTransComparison
            rpt.SetDataSource(das)
            rptViewer.CrystalReportViewer1.ReportSource = rpt
            rptViewer.CrystalReportViewer1.RefreshReport()
            rptViewer.ShowDialog()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim das As New DataSet
            das.Tables.Add("Transactions")
            das.Tables(0).Columns.Add("CustName", GetType(String))
            das.Tables(0).Columns.Add("Writting", GetType(String))
            das.Tables(0).Columns.Add("Acc1", GetType(String))
            das.Tables(0).Columns.Add("Acc2", GetType(String))
            das.Tables(0).Columns.Add("TotalIn", GetType(Double))
            das.Tables(0).Columns.Add("TotalOut", GetType(Double))

            Dim cmd As New SqlCommand("Select Distinct Acc1,Acc2 From Transactions", cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                Dim row As String() = {Me.DTPPeriodTo3.Value.ToShortDateString, Me.DTPPeriodTo4.Value.ToShortDateString, _
                                       Reader.Item(0).ToString, Reader.Item(1).ToString, _
                                       GetAccClosingBalance(Reader.Item(0).ToString, Reader.Item(1).ToString, _
                                                          Me.DTPPeriodTo3.Value.ToShortDateString), _
                                       GetAccClosingBalance(Reader.Item(0).ToString, Reader.Item(1).ToString, _
                                                          Me.DTPPeriodTo4.Value.ToShortDateString)}
                das.Tables(0).Rows.Add(row)
            End While
            cnn.Close()

            Dim rpt As New TrialBalanceClosingComparison
            rpt.SetDataSource(das)
            rptViewer.CrystalReportViewer1.ReportSource = rpt
            rptViewer.CrystalReportViewer1.RefreshReport()
            rptViewer.ShowDialog()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class